<?php

namespace app\admin\model;

use think\Model;

class ImageList extends Model
{
    protected $autoWriteTimestamp = true;

    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
        $page = input('get.page');
        if ($search = input('get.image_title')) $where[] = ['image_title', 'like', "%" . $search . "%"];
        if (input('get.category_id')) $where[] = ['category_id', '=', input('get.category_id')];
        if (input('get.image_status')) $where[] = ['image_status', '=', input('get.image_status') - 1];
        $list = self::order('image_id', 'desc')
            ->where($where)
            ->with(['category'])
            ->paginate($limit);
        foreach ($list as $k => $v) {
            $list[$k]['category_id'] = $v['category'] ? $v['category']['category_title'] : '';
            $list[$k]['show_time'] = $v['is_forever'] ? '长期显示' : $v['show_time'];
        }
        return ['code' => 0, 'data' => $list->items(), 'extend' => ['count' => $list->total(), 'limit' => $limit, 'page' => $page]];
    }

    public function category()
    {
        return $this->hasOne(ImageCategory::class, 'category_id', 'category_id');
    }
}